package com.headline.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.headline.pojo.Headline;
import com.headline.pojo.VO.portalSelectVO;
import com.headline.service.HeadlineService;
import com.headline.mapper.HeadlineMapper;
import com.headline.utils.JwtHelper;
import com.headline.utils.Result;
import com.headline.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 断念残情
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-04-11 17:20:21
*/
@Service
@Slf4j
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;
    /**
     * 首页微头条分页查询
     * 根据关键词搜索，将搜索到的结果封装到Result中
     *
     * 注意事项：
     * 1.查询语句需要自定义，携带分页
     * 2.返回的结果应该是List<map></map>
     * 3.关键字为不为空放到动态sql中去判断
     * @param portalSelectVO
     * @return
     */
    @Override
    public Result findNewsPage(portalSelectVO portalSelectVO) {
        IPage<Map> page = new Page(portalSelectVO.getPageNum(),portalSelectVO.getPageSize());
        headlineMapper.pageQuery(page,portalSelectVO);
        //封装查询到的数据到Map中进一步封装
        List<Map> list = page.getRecords();
        Map data = new HashMap();
        data.put("pageData",list);
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getPages());
        data.put("totalSize",page.getTotal());

        Map pageInfo = new HashMap<>();
        pageInfo.put("pageInfo",data);
        return Result.OK(pageInfo);
    }

    /**
     * 查询头条详情
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        //通过id查找到头条详情
        Map data = headlineMapper.showHeadlineDetail(hid);
        if(data != null){
            //使浏览量增加一
            headlineMapper.updatePageViews(hid);
        }
        Map headline = new HashMap();
        headline.put("headline",data);
        return Result.OK(headline);
    }

    @Override
    public Result publish(Headline headline, String token) {
        //验证用户登录是否过期
        if(jwtHelper.isExpiration(token)){
            log.info("登陆过期，token为：{}",token);
            //过期就提示登录过期
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        //填充数据，把headline的数据填充完整
        headline.setPageViews(0);
        headline.setPublisher(jwtHelper.getUserId(token).intValue());
        headline.setCreateTime(new java.util.Date());
        headline.setUpdateTime(new Date());
        headline.setVersion(1);
        headlineMapper.insert(headline);
        return Result.OK(null);
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);

        Map data = new HashMap<>();
        data.put("hid",headline.getHid());
        data.put("title",headline.getTitle());
        data.put("article",headline.getArticle());
        data.put("type",headline.getType());

        Map map = new HashMap();
        map.put("headline",data);
        return Result.OK(map);
    }

    @Override
    public Result updateHeadline(Headline headline) {
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version);
        headline.setUpdateTime(new java.util.Date());

        headlineMapper.updateById(headline);

        return Result.OK(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        headlineMapper.deleteById(hid);
        return Result.OK(null);
    }
}




